package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;

import com.itheima.reggie.pojo.Employee;
import com.itheima.reggie.service.EmployeeService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;


@RestController
@RequestMapping("employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    //登录
    @PostMapping("login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest request) {
        //获取账户名和密码
        String username = employee.getUsername();
        String password = employee.getPassword();
        //判断账户名
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        QueryWrapper<Employee> qw = queryWrapper.eq("username", username);
        Employee e = employeeService.getOne(qw);
        if (e == null) {
            return R.error("账户名不存在");
        }
        //判断密码
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!e.getPassword().equals(password)) {
            return R.error("密码错误");
        }

        //判断状态
        if (e.getStatus() == 0) {
            return R.error("账户已被禁用");
        }
        //登陆成功,存入域中
        request.getSession().setAttribute("employee", e.getId());

        return R.success(e);
    }

    //退出登录
    @PostMapping("logout")
    public R<String> logout(HttpServletRequest request) {

        request.getSession().invalidate();
        return R.success("退出成功");
    }


    //添加员工
    @PostMapping
    public R<String> save(@RequestBody Employee employee, HttpServletRequest request) {
        //设置初始密码123456，需要进行md5加密处理
        String s = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(s);

        //获取时间
        //employee.setCreateTime(LocalDateTime.now());
        //employee.setUpdateTime(LocalDateTime.now());

        //获得当前登录用户的id
        //Long empId = (Long) request.getSession().getAttribute("employee");

        //employee.setCreateUser(empId);
        //employee.setUpdateUser(empId);
        employeeService.save(employee);
        return R.success("添加成功");
    }

    //分页查询
    @GetMapping("page")
    public R<Page<Employee>> page(Integer page, Integer pageSize, String name) {
        //构造分页构造器
        Page<Employee> pageInfo = new Page<>();
        //构造条件构造器
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        //添加过滤条件
        queryWrapper.like(StringUtils.isNotBlank(name), "name", name);
        //添加排序条件
        queryWrapper.orderByDesc("update_time");
        //执行查询
        employeeService.page(pageInfo, queryWrapper);
        //返回分页数据
        return R.success(pageInfo);
    }

    /**
     * 根据id修改员工信息
     */
    @PutMapping
    public R<String> update(@RequestBody Employee employee, HttpServletRequest request) {
        //创建修改时间
        //employee.setUpdateTime(LocalDateTime.now());
        //获得修改人id
        //Long empId = (Long) request.getSession().getAttribute("employee");
        //设置修改人
        //employee.setUpdateUser(empId);

        employeeService.updateById(employee);
        return R.success("员工信息修改成功");
    }

    /**
     * 根据id查询员工信息
     */
    /*@GetMapping("{id}")
    public R<Employee> getById(@PathVariable Long id) {
        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }*/
    @GetMapping("{id}")
    public R<Employee> getById(@PathVariable Long id) {
        Employee byId = employeeService.getById(id);

        return R.success(byId);
    }
}
